<!--#include file="./header.inc"-->
<%
' KindEditor ASP
' 本ASP程序是演示程序，建议不要直接在实际项目中使用。
' 如果您确定直接使用本程序，使用之前请仔细确认相关安全设置。
dim file_manage
set file_manage = new fileManage
file_manage.upFiles()

db.close()

class fileManage
	public function upFiles()
		dim rootPath, rootUrl
		dim currentPath, currentUrl, currentDirPath, moveupDirPath
		dim path, order, folder, dir, file, result
		dim fileExt, dirCount, fileCount, orderIndex, i, j
		dim dirList(), fileList(), isDir, hasFile, filesize, fileTypes, isPhoto, filetype, filename, datetime

		'根目录路径，可以指定绝对路径，比如 /var/www/attached/
		rootPath = "../" & uploadDir & "/"
		'根目录URL，可以指定绝对路径，比如 http://www.yoursite.com/attached/
		rootUrl = siteUrl & uploadDir & "/"
		'图片扩展名
		fileTypes = "gif,jpg,jpeg,png,bmp"
		
		currentPath = ""
		currentUrl = ""
		currentDirPath = ""
		moveupDirPath = ""

		'根据path参数，设置各路径和URL
		path = request("path")
		path = replace(path, "//", "/")
        '不允许使用..移动到上一级目录
        'if path = "../" then
            'app.json "禁止访问！", 1
        'end if
		if path = "" or path = "../" then
			currentPath = server.mapPath(rootPath) & "/"
			currentUrl = rootUrl
			currentDirPath = rootPath
			moveupDirPath = rootPath
		else
            currentPath = server.mapPath(path) & "/"
            currentUrl = path
            currentDirPath = path
            moveupDirPath = regexReplace(currentDirPath, "(.*?)[^\/]+\/$", "$1")
		end if
		'目录不存在或不是目录
		if not DirectoryExists(currentPath) then
			app.json "目录不存在或不是目录！", 1
		end if
		set folder = fso.getFolder(currentPath)
		'排序形式，name or size or type
		order = lcase(request("order"))
		select case order
			case "type" orderIndex = 4
			case "size" orderIndex = 2
			case else  orderIndex = 5
		end select
		
        '最后一个字符不是/
        'if right(path, 1) <> "/" then
            'app.json "参数错误！", 1
            'path = path & "/"
        'end if

		set result = jsObject()
		result("error") = 0
		'相对于根目录的上一级目录
		result("moveup_dir_path") = moveupDirPath
		'相对于根目录的当前目录
		result("current_dir_path") = currentDirPath
		'当前目录的URL
		result("current_url") = currentUrl
		
		'文件数
		dirCount = folder.subFolders.count
		fileCount = folder.files.count
		result("total_count") = dirCount + fileCount
		
		redim dirList(dirCount)
		i = 0
		for each dir in folder.subFolders
			isDir = true
			hasFile = (dir.files.count > 0)
			filesize = 0
			isPhoto = false
			filetype = ""
			filename = dir.Name
			datetime = FormatDate(dir.DateLastModified)
			dirList(i) = array(isDir, hasFile, filesize, isPhoto, filetype, filename, datetime)
			i = i + 1
		next
		redim fileList(fileCount)
		i = 0
		for each File in folder.files
			fileExt = lcase(mid(File.Name, inStrRev(File.Name, ".") + 1))
			isDir = false
			hasFile = false
			filesize = File.Size
			isPhoto = (inStr(lcase(fileTypes), fileExt) > 0)
			filetype = fileExt
			filename = File.Name
			datetime = FormatDate(File.DateLastModified)
			fileList(i) = array(isDir, hasFile, filesize, isPhoto, filetype, filename, datetime)
			i = i + 1
		next
		'排序
		dim minidx, temp
		for i = 0 to dirCount - 2
			minidx = i
			for j = i + 1 to dirCount - 1
				if (dirList(minidx)(5) > dirList(j)(5)) then
					minidx = j
				end if
			next
			if minidx <> i then
				temp = dirList(minidx)
				dirList(minidx) = dirList(i)
				dirList(i) = temp
			end if
		next
		for i = 0 to fileCount - 2
			minidx = i
			for j = i + 1 to fileCount - 1
				if (fileList(minidx)(orderIndex) > fileList(j)(orderIndex)) then
					minidx = j
				end if
			next
			if minidx <> i then
				temp = fileList(minidx)
				fileList(minidx) = fileList(i)
				fileList(i) = temp
			end if
		next
		set result("file_list") = jsArray()
		for i = 0 to dirCount - 1
			set result("file_list")(null) = jsObject()
			result("file_list")(null)("is_dir") = dirList(i)(0)
			result("file_list")(null)("has_file") = dirList(i)(1)
			result("file_list")(null)("filesize") = dirList(i)(2)
			result("file_list")(null)("is_photo") = dirList(i)(3)
			result("file_list")(null)("filetype") = dirList(i)(4)
			result("file_list")(null)("filename") = dirList(i)(5)
			result("file_list")(null)("datetime") = dirList(i)(6)
		next
		for i = 0 to fileCount - 1
			set result("file_list")(null) = jsObject()
			result("file_list")(null)("is_dir") = fileList(i)(0)
			result("file_list")(null)("has_file") = fileList(i)(1)
			result("file_list")(null)("filesize") = fileList(i)(2)
			result("file_list")(null)("is_photo") = fileList(i)(3)
			result("file_list")(null)("filetype") = fileList(i)(4)
			result("file_list")(null)("filename") = fileList(i)(5)
			result("file_list")(null)("datetime") = fileList(i)(6)
		next
		'输出JSON字符串
		response.addHeader "content-Type", "application/json; charset=UTF-8"
		result.flush
		response.end
	end function
	
	'自定义函数
	private function DirectoryExists(dirPath)
		DirectoryExists = fso.FolderExists(dirPath)
	end function
	
	private function RegexIsMatch(subject, pattern)
		re.pattern = pattern
		RegexIsMatch = re.test(subject)
	end function
	
	private function regexReplace(subject, pattern, replacement)
		re.pattern = pattern
		regexReplace = re.replace(subject, replacement)
	end function
	
	private function FormatDate(datetime)
		dim y, m, d, h, i, s
		y = cstr(year(datetime))
		m = cstr(month(datetime))
		if len(m) = 1 then m = "0" & m
		d = cstr(day(datetime))
		if len(d) = 1 then d = "0" & d
		h = cstr(hour(datetime))
		if len(h) = 1 then h = "0" & h
		i = cstr(minute(datetime))
		if len(i) = 1 then i = "0" & i
		s = cstr(second(datetime))
		if len(s) = 1 then s = "0" & s
		FormatDate = y & "-" & m & "-" & d & " " & h & ":" & i & ":" & s
	end function

end class
%>